package kz.vokebuz.dataStore;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class DbOpenHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "smsdb";
	public static final String TABLE_SMS = "smslog";
	private static final String TAG = DbOpenHelper.class.getSimpleName();
	Context context;

	private final String TABLE_CREATE = "create table " + TABLE_SMS + " ("
			+ SmsMmsMsg.COLUMN_ID + " integer primary key autoincrement , "
			+ SmsMmsMsg.COLUMN_TIMESTAMP + " integer , "
			+ SmsMmsMsg.COLUMN_SENDER + " text, " 
			+ SmsMmsMsg.COLUMN_TEXT + " text, " 
			+ SmsMmsMsg.COLUMN_MSG_TYPE + " text ,"
			+ SmsMmsMsg.COLUMN_STATUS + " text ," 
			+ SmsMmsMsg.COLUMN_CATHID + " integer, " 
			+ SmsMmsMsg.COLUMN_LOCKED + " integer, "
			+ SmsMmsMsg.COLUMN_PROTOCOL + "text, " 
			+ SmsMmsMsg.COLUMN_THREADID + "integer, " 
			+ SmsMmsMsg.COLUMN_SUBJECT + " text " + ");";

	public DbOpenHelper(Context context) {

		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
		db.execSQL("create table dual (x text);");
		db.execSQL("insert into dual(x) values ('x');");
		grabExistSms(db);

	}

	private void grabExistSms(SQLiteDatabase db) {
		ContentValues values = new ContentValues();

		Uri uriSMSURI = Uri.parse("content://sms/");
		Cursor cursor = context.getContentResolver()
				.query(uriSMSURI,
						new String[] { "thread_id", "address",
								"date", "body", "read","protocol","status","type","person"}, null, null, null);

		if (cursor != null) {
			try {
				int count = cursor.getCount();
				if (count > 0) {
					cursor.moveToFirst();
					while (cursor.isAfterLast() == false) {
						if (values.size()>0) {
							values.clear();
						}
						values.put(SmsMmsMsg.COLUMN_CATHID, cursor.getLong(0));
						values.put(SmsMmsMsg.COLUMN_SENDER, cursor.getString(1));
						values.put(SmsMmsMsg.COLUMN_TIMESTAMP, cursor.getLong(2));
						values.put(SmsMmsMsg.COLUMN_TEXT, cursor.getString(3));
						
						//FOR TEST
						StringBuffer info = new StringBuffer();
						info.append("_read : " + cursor.getString(4) + "\n");
						info.append("protocol :" + cursor.getString(5) + "\n");
						info.append("status :" + cursor.getString(6)+ "\n");
						info.append("type : " + cursor.getString(7)+ "\n");
						info.append("peson :" +cursor.getString(8) +"\n");
						
						
						
						long result = db.insert(DbOpenHelper.TABLE_SMS, null, values);
						Log.d(TAG, "insertion" + result + ""); 
						cursor.moveToNext();
                    }
					
				}
			} finally {
				cursor.close();
			}

		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS todo");
		onCreate(db);
	}

}
